/**
 * Reinit error visibility
 */
function reinitErrorVisibility() {
	// Error messages reinit
	$("#errorRppsNumber").hide();
	$("#errorAdeliNumber").hide();
	$("#errorPostalCode").hide();
	$("#noProfessionalFound").hide();
	$("#errorAdeliNumberFormat").hide();
	$("#errorRppsNumberFormat").hide();
	$("#errorPasswordMatch").hide();
	$("#errorPasswordTooShort").hide();
	$("#errorMailRegex").hide();
	$("#errorGettingInfos").hide();
	$("#errorList").hide();
	$("#errorAddressUnknown").hide();
	$("#lat").val('');
	$("#lng").val('');
	unHighlightFields();
}

/*
 * Process a json response
 */
function processJson(json) {
	$.unblockUI({
		fadeOut : 200
	});
	if (json != null) {
		$("#professional_number").hide('slow');
		$("#user_datas").show('slow');
		$("#rppsNumber").val(json.rppsNumber);
		$("#adeliNumber").val(json.adeliNumber);
		$("#firstname").val(json.firstname);
		$("#lastname").val(json.lastname);
		if (null != json.rppsNumber) {
			$("#username").val(json.rppsNumber);
		} else {
			$("#username").val(json.adeliNumber);
		}
		$("#password").val("");
		$("#passwordConfirms").val("");
		for ( var i = 0; i < 3; i++) {
			if ($("input:radio[name=gender]:nth(" + i + ")").val() == json.gender) {
				$("input:radio[name=gender]:nth(" + i + ")").attr('checked',
						true);
			}
		}
		$("#speciality").val(json.speciality);
		$("#job").val(json.job);
		$("#postalAddress").val(json.postalAddress);
		$("#postalCode").val(json.postalCode);
		$("#city").val(json.city);
		$("#postOfficeBox").val(json.postOfficeBox);
		$("#departmentCode").val(json.departmentCode);
		$("#departmentLabel").val(json.departmentLabel);
		$("#corporateName").val(json.corporateName);
	} else {
		$("#noProfessionalFound").show('fast');
	}
}

/*
 * Search for a professional
 */
function searchProfessional() {
	var jsonUrl = applicationUrl + "/do/ajax/find-professional";
	reinitErrorVisibility();
	var adeliNumber = $("#adeli_number").val();
	var postalCode = $("#postal_code").val();
	var rppsNumber = $("#rpps_number").val();
	// If everything is empty
	if (rppsNumber.length == 0 && adeliNumber.length == 0
			&& postalCode.length == 0) {
		highlightField($("#rpps_number"));
		highlightField($("#adeli_number"));
		highlightField($("#postal_code"));
	}
	// If the postal code is not numeric, we print an error
	else if (isNaN(rppsNumber) == true) {
		$("#errorRppsNumberFormat").show();
		highlightField($("#rpps_number"));
	}
	// We print an error if the length is not correct
	else if (rppsNumber.length > 0 && rppsNumber.length < 11) {
		$("#errorRppsNumber").show();
		highlightField($("#rpps_number"));
	}
	// If the RPPS is OK
	else if (rppsNumber.length >= 11) {
		waitForResult();
		if (rppsNumber.length == 12) {
			rppsNumber = rppsNumber.substring(1);
		}
		var jsonObject = $.getJSON(jsonUrl, {
			rppsNumber : rppsNumber
		}, function(json) {
			processJson(json);
		});
		jsonObject.error(function() {
			$.unblockUI({
				fadeOut : 200
			});
			$("#errorGettingInfos").show();
		});
	}
	// If one of the adeli number or the postal code is not
	// empty
	// If ADELI number is empty we focus on the field
	else if (adeliNumber.length == 0) {
		$("#adeli_number").focus();
		highlightField($("#adeli_number"));
	}
	// If the postal code is not numeric, we print an error
	else if (isNaN(adeliNumber) == true) {
		$("#errorAdeliNumberFormat").show();
		highlightField($("#adeli_number"));
	}
	// If the ADELI number is too short, we print an error
	else if (adeliNumber.length < 9) {
		$("#errorAdeliNumber").show();
		highlightField($("#adeli_number"));
	}
	// If the postal code is empty we focus on the field
	else if (postalCode.length == 0) {
		highlightField("#postal_code");
	}
	// If the postal code is not numeric, we print an error
	else if (isNaN(postalCode) == true) {
		$("#errorPostalCode").show();
		highlightField("#postal_code");
	}
	// If the ADELI and postal code are OK
	else {
		waitForResult();
		if (adeliNumber.length == 10) {
			adeliNumber = adeliNumber.substring(1);
		}
		$.getJSON(jsonUrl, {
			adeliNumber : adeliNumber,
			postalCode : postalCode
		}, function(json) {
			processJson(json);
		});
	}
	return false;
}

/*
 * Keyboard events
 */
$(function() {
	$("#professional_number input").keypress(function(e) {
		if (e.which == 13) {
			searchProfessional();
		}
	});
	$("#search_for_professional").click(function() {
		searchProfessional();
	});
});

/*
 * Registration submit
 */
$(function() {
	$("#register_button").click(
			function(e) {
				waitForResult();
				reinitErrorVisibility();
				searchAddress($('#postalAddress').val() + ', '
						+ $('#postalCode').val() + ' ' + $('#city').val());
			});
});